home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / oop_tp55.zip / LIST4_1.PAS < prev    next >
Pascal/Delphi Source File  |  1990-01-09  |  11KB  |  381 lines

  1. { Unit : PLOTDATA
  2.  
  3.   Utilization notes:
  4.  
  5. 1. For informational purposes only, there is a 4-pixel
  6.    border between the edge of a plot viewport and the
  7.    actual area occupied by the plot.
  8.  
  9. }
  10. { Specification for Plot class ****************************
  11.  
  12.   Variables:
  13.             x1 : integer;
  14.             Coordinate (global) of the left edge of the
  15.             Plot object's viewport.
  16.  
  17.             x2 : integer;
  18.             Coordinate (global) of the right edge of the
  19.             Plot object's viewport.
  20.  
  21.             y1 : integer;
  22.             Coordinate (global) of the top edge of the
  23.             Plot object's viewport.
  24.  
  25.             y2 : integer;
  26.             Coordinate (global) of the bottom edge of the
  27.             Plot object's viewport.
  28.  
  29.             Mnx : real;
  30.             Minimum x axis scale value.
  31.  
  32.             Mny : real;
  33.             Minimum y axis scale value.
  34.  
  35.             Mxx : real;
  36.             Maximum y axis scale value.
  37.  
  38.             Mxy : real;
  39.             Maximum y axis scale value.
  40.  
  41.             XLabel : string;
  42.             Label for x axis.
  43.  
  44.             YLabel : string;
  45.             Label for y axis.
  46.  
  47.             OldPoint : Point;
  48.             Object type that stores the local viewport
  49.             coordinates of the last point plotted.
  50.             Set to (0,0,FALSE) during initialization.
  51.  
  52.             OriginX : integer;
  53.             Viewport x coord of origin.
  54.  
  55.             OriginY : integer;
  56.             Viewport y coord of origin.
  57.  
  58.             MaxXAxis : integer;
  59.             Viewport x coordinate of the maximum x-axis
  60.             endpoint.
  61.  
  62.             MaxYAxis : integer;
  63.             Viewport y coordinate of the maximum y-axis
  64.             endpoint.
  65.  
  66.             DeltaXAxis : integer;
  67.             Equal to MaxXAxis - OriginX.
  68.  
  69.             DeltaYAxis : integer;
  70.             Equal to OriginY - MaxYAxis.
  71.  
  72.             DeltaX : real;
  73.             Equal to Mxx - Mnx.
  74.  
  75.             DeltaY : real;
  76.             Equal to Mxy - Mny.
  77.  
  78.   Procedures:
  79.             .Init( miny, maxy, minx, maxx : real;
  80.                    lft, rt, tp, btm: integer;
  81.                    xlbl, ylbl : string );
  82.             Store all viewport parameters, maxima and
  83.             minima for both axes.  Calculate all "deltas,"
  84.             set viewport with border, draw axes with
  85.             labels and maximum and minimum scale values.
  86.  
  87.             .AddPoint( x, y : real );
  88.             Figure out where in the plot area the new point
  89.             is located, and draw a line between the point
  90.             stored in OldPoint and the new point.
  91.  
  92.   Descendant object types:
  93.             None.               }
  94.  
  95. unit PlotData;
  96.  
  97. interface
  98.  
  99. uses Graph, Crt, Points;
  100.  
  101. const
  102.  
  103. LBuf = 4;
  104. RBuf = 4;
  105. TBuf = 4;
  106. BBuf = 4;
  107. left = 3;
  108. right = 1;
  109. top = 0;
  110. bottom = 2;
  111.  
  112. type
  113.  
  114. Plot = object
  115.        x1, x2, y1, y2 : integer; { viewport left, right, top, bottom }
  116.        Mnx, Mny, Mxx, Mxy : real; { min/max x min/max y }
  117.        XW,XD,YW,YD : integer;
  118.        XSFac, YSFac : real;
  119.        XLabel : string;
  120.        YLabel : string;
  121.        PlotTitle : string;
  122.        OldPoint : Point; { last point plotted }
  123.        OriginX : integer; { viewport x coord of origin }
  124.        OriginY : integer; { viewport y coord of origin }
  125.        MaxXAxis : integer;
  126.        MaxYAxis : integer;
  127.        DeltaXAxis : integer;
  128.        DeltaYAxis : integer;
  129.        DeltaX : real;
  130.        DeltaY : real;
  131.        PlotEnabled : boolean;
  132.        procedure Init( miny, maxy, ysf, minx, maxx, xsf : real;
  133.                        lft, rt, tp, btm, wx, dx, wy, dy : integer;
  134.                        ptitle, xlbl, ylbl : string;
  135.                        penbl : boolean );
  136.        procedure AddPoint( x, y : real );
  137.        procedure DrawHGridLine( y : real );
  138.        procedure DrawVGridLine( x : real );
  139.        procedure PlaceXAxisValue( r : real );
  140.        procedure PlaceYAxisValue( r : real );
  141.        procedure DrawTickMark( x, y : integer );
  142.        end;
  143.  
  144.  
  145.  
  146. implementation
  147.  
  148. procedure Plot.Init( miny, maxy, ysf, minx, maxx, xsf : real;
  149.                        lft, rt, tp, btm, wx, dx, wy, dy : integer;
  150.                        ptitle, xlbl, ylbl : string;
  151.                        penbl : boolean );
  152. var
  153.  
  154. LBorder, THeight : integer;
  155. VP : ViewPortType;
  156. TST : TextSettingsType;
  157. s : array[0..3] of string[11];
  158. begin
  159.      XW := wx;     XD := dx;
  160.      YW := wy;     YD := dy;
  161.      XSFac := xsf;
  162.      YSFac := ysf;
  163.      Xlabel := xlbl;
  164.      Ylabel := ylbl;
  165.      PlotTitle := ptitle;
  166.      x1 := lft;
  167.      x2 := rt;
  168.      y1 := tp;
  169.      y2 := btm;
  170.      Mnx := minx*xsf;
  171.      Mxx := maxx*xsf;
  172.      Mny := miny*ysf;
  173.      Mxy := maxy*ysf;
  174.      DeltaX := Mxx - Mnx;
  175.      DeltaY := Mxy - Mny;
  176.      Str( maxx:XW:XD, s[right]);
  177.      Str( maxy:YW:YD, s[top]);
  178.      Str( minx:XW:XD, s[left]);
  179.      Str( miny:YW:YD, s[bottom]);
  180.      THeight := TextHeight(s[0]);
  181.      if TextWidth( s[top] ) < TextWidth( s[bottom]) then
  182.         LBorder := TextWidth(s[bottom])
  183.      else
  184.         LBorder := TextWidth(s[top]);
  185.  
  186.      SetViewPort( lft, tp, rt, btm, true);
  187.      if GraphResult = grError then
  188.         OutTextXY( 0,0,'Input error to SetViewPort in Plot.Init' );
  189.      GetViewSettings( VP );
  190.      with VP do
  191.      begin
  192.           SetFillStyle( SolidFill, blue );
  193.           Rectangle(0,0,x2-x1, y2-y1);
  194.           FloodFill( 1,1, GetMaxColor );
  195.  
  196.           OriginY := y2-y1-2*(BBuf+THeight);
  197.           OriginX := LBuf+LBorder+THeight;
  198.           MaxYAxis := TBuf+(3*THeight div 2);
  199.           MaxXAxis := x2-x1-RBuf-(TextWidth(s[right]) div 2);
  200.           DeltaXAxis := MaxXAxis - OriginX;
  201.           DeltaYAxis := OriginY - MaxYAxis;
  202.  
  203.           GetTextSettings( TST );
  204.           with TST do
  205.           begin
  206.                Line( OriginX, MaxYAxis, OriginX, OriginY);
  207.                SetTextJustify(RightText, BottomText);
  208.                MoveTo(OriginX, OriginY);
  209.                OutText(s[bottom]);
  210.                MoveTo(OriginX,TBuf+(2*THeight));
  211.                OutText(s[top]);
  212.  
  213.                Line( OriginX, OriginY,MaxXAxis,OriginY);
  214.                SetTextJustify(CenterText, BottomText);
  215.                MoveTo(OriginX, y2-y1-BBuf-THeight);
  216.                OutText(s[left]);
  217.                MoveTo( MaxXAxis, y2-y1-BBuf-THeight);
  218.                OutText(s[right]);
  219.  
  220.                MoveTo(OriginX+DeltaXAxis div 2, y2-y1-BBuf);
  221.                OutText( XLabel );
  222.                SetTextStyle( Font, VertDir, CharSize );
  223.                MoveTo( LBuf+(THeight div 2),
  224.                        OriginY-(DeltaYAxis div 2) );
  225.                SetTextJustify( CenterText, CenterText );
  226.                OutText(YLabel);
  227.                SetTextStyle( Font, Direction, CharSize );
  228.                MoveTo( (x2-x1) div 2, TBuf+(THeight div 2));
  229.                OutText(PlotTitle);
  230.                SetTextJustify( Horiz, Vert );
  231.           end;
  232.      end;
  233.      OldPoint.Init(OriginX,OriginY);
  234.      PlotEnabled := PEnbl;
  235. end;
  236.  
  237. procedure Plot.AddPoint( x, y : real );
  238. var
  239.    VP : ViewPortType;
  240.    px, py : integer;
  241. begin
  242.      GetViewSettings( VP );
  243.      SetViewPort( x1, y1, x2, y2, true );
  244.      px := OriginX+Round(((x-mnx)/DeltaX)*DeltaXAxis);
  245.      py := OriginY-Round(((y-mny)/DeltaY)*DeltaYAxis);
  246.      if (x >= mnx) and
  247.         (x <= mxx) and
  248.         (y >= mny) and
  249.         (y <= mxy) then
  250.           begin
  251.           with OldPoint do
  252.                begin
  253.                if PlotEnabled then
  254.                   line( OldPoint.X, OldPoint.Y, px, py );
  255.                OldPoint.Init( px, py );
  256.                PlotEnabled := true;
  257.                end;
  258.           end;
  259.      with VP do
  260.           SetViewPort( x1, y1, x2, y2, true );
  261. end;
  262.  
  263. procedure Plot.DrawHGridLine( y : real );
  264. var
  265.    VP : ViewPortType;
  266.    LST : LineSettingsType;
  267.    py : integer;
  268. begin
  269.      GetViewSettings( VP );
  270.      SetViewPort( x1, y1, x2, y2, true );
  271.      if (y > mny) and (y < mxy) then
  272.         begin
  273.         py := OriginY-Round(((y-mny)/DeltaY)*DeltaYAxis);
  274.         GetLineSettings( LST );
  275.         with LST do
  276.              begin
  277.              SetLineStyle( DottedLn, Pattern, NormWidth );
  278.              Line( OriginX, py, MaxXAxis, py );
  279.              SetLineStyle( LineStyle, Pattern, Thickness );
  280.              end;
  281.         end;
  282.      with VP do
  283.           SetViewPort( x1, y1, x2, y2, true );
  284. end;
  285.  
  286. procedure Plot.DrawVGridLine( x : real );
  287. var
  288.    VP : ViewPortType;
  289.    LST : LineSettingsType;
  290.    px : integer;
  291. begin
  292.      GetViewSettings( VP );
  293.      SetViewPort( x1, y1, x2, y2, true );
  294.      if (x > mnx) and (x < mxx) then
  295.         begin
  296.         px := OriginX+Round(((x-mnx)/DeltaX)*DeltaXAxis);
  297.         GetLineSettings( LST );
  298.         with LST do
  299.              begin
  300.              SetLineStyle( DottedLn, Pattern, NormWidth );
  301.              Line( px, OriginY, px, MaxYAxis );
  302.              SetLineStyle( LineStyle, Pattern, Thickness );
  303.              end;
  304.         end;
  305.      with VP do
  306.           SetViewPort( x1, y1, x2, y2, true );
  307. end;
  308.  
  309. procedure Plot.DrawTickMark( x, y : integer );
  310. var
  311.    VP : ViewPortType;
  312. begin
  313.      GetViewSettings( VP );
  314.      SetViewPort( x1, y1, x2, y2, true );
  315.      PutPixel( x, y, GetColor );
  316.      PutPixel( x+1, y, GetColor );
  317.      PutPixel( x, y+1, GetColor );
  318.      PutPixel( x+1, y+1, GetColor );
  319.      with VP do
  320.           SetViewPort( x1, y1, x2, y2, true );
  321. end;
  322.  
  323. procedure Plot.PlaceXAxisValue( r : real );
  324. var
  325.    VP : ViewPortType;
  326.    TST : TextSettingsType;
  327.    px : integer;
  328.    s  : string;
  329. begin
  330.      GetViewSettings( VP );
  331.      SetViewPort( x1, y1, x2, y2, true );
  332.      if (r > mnx) and (r < mxx) then
  333.         begin
  334.         GetTextSettings( TST );
  335.         Str( (r/XSFac):XW:XD, s );
  336.         px := OriginX+Round(((r-mnx)/DeltaX)*DeltaXAxis);
  337.         SetTextJustify( CenterText, BottomText);
  338.         MoveTo( px, y2-y1-BBuf-TextHeight(s));
  339.         OutText(s);
  340.         with TST do
  341.              SetTextJustify( Horiz, Vert );
  342.         DrawTickMark( px, OriginY-1 );
  343.         end;
  344.      with VP do
  345.           SetViewPort( x1, y1, x2, y2, true );
  346. end;
  347.  
  348. procedure Plot.PlaceYAxisValue( r : real );
  349. var
  350.    VP : ViewPortType;
  351.    TST : TextSettingsType;
  352.    py : integer;
  353.    s  : string;
  354. begin
  355.      GetViewSettings( VP );
  356.      SetViewPort( x1, y1, x2, y2, true );
  357.      if (r > mny) and (r < mxy) then
  358.         begin
  359.         GetTextSettings( TST );
  360.         Str( (r/YSFac):YW:YD, s );
  361.         py := OriginY-Round(((r-mny)/DeltaY)*DeltaYAxis);
  362.         SetTextJustify( RightText, CenterText);
  363.         MoveTo( OriginX, py );
  364.         OutText(s);
  365.         with TST do
  366.              SetTextJustify( Horiz, Vert );
  367.         DrawTickMark( OriginX+1, py-1 );
  368.         end;
  369.      with VP do
  370.           SetViewPort( x1, y1, x2, y2, true );
  371. end;
  372.  
  373. { Initialization code }
  374. begin
  375.  
  376. { None }
  377.  
  378. end.
  379.  
  380. { Listing 4-1 should be saved and compiled as PLOTDATA.PAS }
  381.